﻿<%@ WebHandler Language="C#" Class="PrintBill" %>

using System;
using System.Web;
using Chenxin.Lib.Utility;

public class PrintBill : AjaxHandlerBase {

    public PrintBill()
    {
        htTypePermit.Add("printview", Constants.Permit_PrintView);
        htTypePermit.Add("reprintview", Constants.Permit_RePrintview);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //个人业绩排行
                case "getprintmodel":
                    strRet = GetPrintModel(context);
                    break;
                case "saveprint":
                    strRet = SavePrintInfo(context);
                    break;
                default:
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    private string SavePrintInfo(HttpContext context)
    {
        string billId = context.Request["billId"];
        Chenxin.Lib.Business.Entity.t_Contract_Finance_BillPrintInfo mode = new Chenxin.Lib.Business.Entity.t_Contract_Finance_BillPrintInfo();
        mode.RecordId = Guid.NewGuid();
        mode.BillId = new Guid(billId);
        mode.BillCode = context.Request["billCode"];
        mode.PrintDepart = CurrentUserInfo.DepartName;
        mode.PrintDepartId = CurrentUserInfo.DepartId;
        mode.PrintTime = DateTime.Now;
        mode.PrintEmployee = CurrentUserInfo.UserName + "(" + CurrentUserInfo.UserCode + ")";
        mode.PrintEmployeeId = CurrentUserInfo.UserId;

        Chenxin.Lib.Business.BLL.t_Contract_Finance_BillPrintInfo.Add(mode);

        string text = CurrentUserInfo.UserName + "：开单打印。单据编号：" + mode.BillCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_PrintBill"].ToString());
        
        return retExecResult(Constants.ExecResult.Succ, "成功", "");
    }
    
    /// <summary>
    /// 获取Bill
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    private string GetPrintModel(HttpContext context)
    {
        string billId = (string.IsNullOrEmpty(context.Request["billId"])) ? Guid.Empty.ToString() : context.Request["billId"];

        if (string.IsNullOrEmpty(billId)) return retExecResult(Constants.ExecResult.Error, "开单标识丢失", "");
        Chenxin.Lib.Business.Entity.v_PrintBillInfo model = Chenxin.Lib.Business.BLL.v_PrintBillInfo.GetModel(new Guid(billId));
        if (model == null) return retExecResult(Constants.ExecResult.Error, "获取开单信息失败，请联系管理员！", "");

        model.PrintDate = string.Format("{0:yyyy-MM-dd}", DateTime.Today);
        string str = "";
        Chenxin.Lib.Utility.InitialHelper.TranslateNumberToChinese(model.BillMoney.ToString(), out str);
        model.BillMoneyBig = str;
        model.PayMode = model.PayBankType + "/" + model.PayBankName + "/" + model.PayBankNo;
        if (model.FundName != "代收房款" && model.FundName != "代收税费") model.FundName = "中介服务费";
        
        return string.Format("{{success:1,msg:'',data:{{model:{0}}}}}", JsonHelp.Convert2Json(model));
    }
}